﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MedicViet.Models;

namespace MedicViet.Controllers
{
    public class AboutController : Controller
    {
        MedicVietEntities db = new MedicVietEntities();

        public ActionResult Index()
        {
            var about = (from c in db.mvCategories.Where(cat => cat.name=="About")
                           from p in db.mvNews.Where(news => news.mvCategories.Contains(c))
                           orderby p.dateCreate
                           descending
                           select p).FirstOrDefault();
            return View(about);
        }

        public ActionResult Details(int id)
        {
            var about = (from A in db.mvNews
                         where A.id == id
                         select A).FirstOrDefault();

            return View(about);
        }


        public ActionResult Edit()
        {
            var about = (from c in db.mvCategories.Where(cat => cat.name == "About")
                            from p in db.mvNews.Where(news => news.mvCategories.Contains(c))
                            select p).FirstOrDefault();
            return View(about);
        }
        [HttpPost]
        public ActionResult Edit(mvNew mvAbout)
        {
            mvNew about = (from c in db.mvCategories.Where(cat => cat.name == "About")
                                from p in db.mvNews.Where(news => news.mvCategories.Contains(c))
                                select p).FirstOrDefault();

            about.title = mvAbout.title;
            about.nContent = mvAbout.nContent;
            about.dateModified = DateTime.Now;
            about.description = mvAbout.description;

            db.SaveChanges();

            return RedirectToAction("Index", "About");
        }

    }
}
